package cn.rumoss.nettytest.time.pojo;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class TimeServerHandler extends ChannelHandlerAdapter{
  
  /**
   * 连接被建立并且准备进行通信时被调用
   */
  @Override
  public void channelActive(final ChannelHandlerContext ctx) throws Exception{
    
    UnixTime date = new UnixTime();
    System.out.println("服务端准备发往客户端： " + date);
    ChannelFuture f = ctx.writeAndFlush(date);
    f.addListener(ChannelFutureListener.CLOSE);
    
  }
  
  @Override
  public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) throws Exception{
    
    // 当异常发生的时候关闭连接
    cause.printStackTrace();
    ctx.close();
    
  }
  
}
